package com.simplemonkey.tooltip;

import com.jme.scene.Node;
import com.jme.scene.Spatial.CullHint;
import com.simplemonkey.Container;
import com.simplemonkey.IWidget;
import com.simplemonkey.layout.BorderLayout;
import com.simplemonkey.widgets.TextWidget;

/**
 * A default implementation of IToolTip.
 * Note: You must attach this to your scene scene graph for it to be visible!
 * @author Sam
 *
 */
public class ToolTipWidget extends Container implements IToolTip{


	
	private IWidget owner = null;
	
	private String toolTip = "";

	protected TextWidget toolTipText;
	
	public ToolTipWidget() {
		this("ToolTip");	
	
	}
	
	public ToolTipWidget(String name) {
		super(name,new Node(name),new BorderLayout());
		
		toolTipText = new TextWidget(toolTip);
		addWidget(toolTipText);
		layout();
		
		this.getSpatial().setZOrder(-1024,true);
		
		this.getSpatial().setCullHint(CullHint.Always);
	}

	
	
	public IWidget getOwner() {
		return owner;
	}

	public void setOwner(IWidget owner) {
		this.owner = owner;
	}

	public String getToolTip() {
		return toolTip;
	}

	public void setToolTip(String toolTip) {
		this.toolTip = toolTip;
	}

	public void showToolTip(IWidget owner, String toolTip, float x, float y) {
		if(toolTip == null)
			this.toolTip = "";
		else
			this.toolTip = toolTip;
		
		if (owner == null)
		{
			//if the new owner is null, just hide the tooltip
			setToolTipVisible(false);
			this.owner= owner;
		}else
		{	
			this.owner = owner;
			setToolTipVisible(true);
			
			this.setWorldPosition(x,y);
			toolTipText.setText(toolTip);
	
			toolTipText.layout();
			layout();//resize the tooltip to fit the new text
			
			//position the tooltip so that it fits on the screen, and is 'near' the owner
			
			
			
		}
		
	}

	public void setToolTipVisible(boolean visible) {
		if(visible)
		{
			this.getSpatial().setCullHint(CullHint.Inherit)	;	
		}else
			this.getSpatial().setCullHint(CullHint.Always);		
		
	}

	public void hideToolTip(IWidget owner) {
		if(this.owner == owner)
		{
			setToolTipVisible(false);
			this.owner = null;
			
		}
		
	}


	
}
